const {
  $util,
} = getApp();
Page({
  data: {
    nav: [{
      title: '待采纳',
      id: 0,
    }, {
      title: '已采纳',
      id: 1,
    }, {
      title: '未采纳',
      id: 2,
    }],
    adminNav: [{
      title: '我提交的',
      id: 0,
    }, {
      title: '待我审批',
      id: 1,
    }],
    openType: 0,
    ctype: 0, // 待采纳 0，已采纳 1和未采纳 2
    page: 1,
    hasMore: true,
    isLoad: false,
    navLoad: false,
  },
  onLoad(opts) {
    this.setData(opts);
    wx.setNavigationBarTitle({
      title: opts.barTitle && decodeURIComponent(opts.barTitle) || '书记信箱',
    });
    Number(opts.ctype || 0) === 1 && this.changeNav({
      detail: { obj: { id: 1 }},
    });
  },
  onShow() {
    this.setData({
      page: 1,
    }, () => {
      Number(this.data.ctype) === 0 ? this.loadSuggestionList() : this.loadList();
    });
    let wxData = wx.getStorageSync('qyUserInfo') || {};
    this.setData(wxData);
    this.setData({
      isSaaS: wx.getStorageSync('isSaaS'),
    });
  },
  changeNav(e) {
    this.setData({
      page: 1,
      ctype: e.detail.obj.id,
      isLoad: false,
      list: [],
      hasMore: true,
    }, () => {
      if (this.data.canshow && e.detail.obj.id === 1) {
        this.loadList();
      } else {
        this.loadSuggestionList();
      }
    });
  },
  deleteSuggestion(e) {
    const idx = e.currentTarget.dataset.idx;
    const D = this.data;
    const list = D.list;
    const id = list[idx].id;
    wx.showModal({
      title: '确认删除',
      content: '确认删除这条提交吗?',
      success: (res) => {
        if (res.confirm) {
          $util.request({
            url: 'entry/wxapp/partyman.home.deleteadvice',
            data: {
              id,
            },
            success: (res) => {
              wx.showToast({
                title: '删除成功',
                icon: 'none',
                mask: true,
              });
              list.splice(idx, 1);
              this.setData({
                list,
              });
            },
            fail: (err) => {
              console.error(err);
            },
          });
        }
      },
    });
  },
  loadSuggestionList() {
    let { openType, ctype, page, list } = this.data;
    $util.request({
      url: 'entry/wxapp/partyman.home.getadvice',
      data: {
        type: openType,
        ctype: ctype,
        page: page,
        version: 1,
      },
      success: (res) => {
        const {
          groupid,
          type2,
          total,
          canshow,
        } = res;

        let lists = res.data;
        if (page > 1) {
          lists = [...list, ...lists];
        }
        this.setData({
          canshow,
          groupid,
          type2,
          total,
          isLoad: true,
          hasMore: res.data.length !== 0,
          page: res.data.length !== 0 ? page + 1 : page,
          list: lists,
          navLoad: true,
        });
      },
      fail: (err) => {
        console.error(err);
      },
    });
  },
  // 管理员 待 采纳
  loadList() {
    let {
      page,
      list,
    } = this.data;
    $util.request({
      url: `entry/wxapp/verify.secretarymail.getList`,
      data: {
        page,
        check: 0,
      },
      success: (res) => {
        let lists = res.list;
        if (page > 1) {
          lists = [...list, ...lists];
        }
        this.setData({
          hasMore: res.list.length !== 0,
          page: res.list.length !== 0 ? page + 1 : page,
          list: lists,
          isLoad: true,
          navLoad: true,
          canshow: true,
        });
      },
    });
  },
  onReachBottom() {
    let { hasMore, ctype } = this.data;
    if (!hasMore) return;
    Number(ctype) === 0 ? this.loadList() : this.loadSuggestionList();
  },
  go(e) {
    let { item, index } = e.currentTarget.dataset;
    let { openType, canshow } = this.data;
    let url = canshow ? `/ws_party_const/pages/verify/detail?isdo=secretarymail&id=${item.id}&idx=${index}&sid=${item.status}&title=建议审批` : `/ws_party_const/pages/suggestion-detail/suggestion-detail?id=${item.id}&openType=${openType}`;
    wx.navigateTo({
      url,
    });
  },
});